let app = getApp();
import { getCorner } from "../../../request/api";
import dayjs from "../../../common/js/dayjs.js";

Page({

    /**
     * 页面的初始数据
     */
    data: {
        lang: null,
        currentItem: 1,
        validateLoginData: false,
        showAlert: false,
        currentItemIndex: null,
        allMessages: [],
        pendingReview: []
    },
    // 全部
    getPost() {
        let pageNo = 1
        let pageSize = 10
        let data = { pageNo, pageSize, getMy:true }
        getCorner(res => {
            if (res.statusCode == 200) {
                for (let item of res.data.data) {
                    item.releaseTime = dayjs(item.releaseTime).format("YYYY-MM-DD")
                }
                this.setData({ allMessages: res.data.data })
            }
        }, data);
    },

    // 待审核
    pendingReview() {
        let status = 0
        let pageNo = 1
        let pageSize = 10
        let data = { status, pageNo, pageSize, getMy:true }
        getCorner(res => {
            if (res.statusCode == 200) {
                for (let item of res.data.data) {
                    item.releaseTime = dayjs(item.releaseTime).format("YYYY-MM-DD")
                }
                this.setData({ pendingReview: res.data.data })
            }
        }, data);
    },

    //用户点击bottomTab时调用
    onClickTopTab(event) {
        let index = event.currentTarget.dataset.idx;
        this.setData({ currentItem: index });
        switch (index) {
            case '1':
                this.getPost()
                break;
            case '2':
                this.pendingReview()
                break;
            default:
                break;
        }
    },

    onClickEdit(event) { },

    /**
     * 删除发布内容
     */
    // 获取 Id
    getIndexById(id, arr) {
        for (let i in arr) {
            if (id == arr[i].id) {
                return i
            }
        }
        return -1;
    },

    // 确定删除对
    alertOnConfirm() {
        let token = null;
        wx.getStorage({
            key: 'userInfo',
            success(res) {
                token = res.data.token
                wx.request({
                    // url:'https://api-vipatush.gzjjt.com.cn/house',
                    url: 'http://127.0.0.1:3000/houseDestroy',
                    header: {
                        token: res.data.token
                    },
                    data: { postId: 48 },
                    method: 'post',
                    success: function (res) {
                        console.log('res: ', res);
                    }
                })
            }
        })

        // 删除请求
        if (this.data.currentItemIndex) {
            let index = this.data.currentItemIndex
            let arr = this.data.allMessages
            arr.splice(index, 1)
            this.setData({ allMessages: arr })
        }
        this.setData({ showAlert: false })
    },

    // 取消删除
    alertOnClsoe() {
        console.log("alert closed!");
        this.setData({ showAlert: false })
    },

    // 弹出对话框
    showAlertTap(e) {
        let item = e.currentTarget.dataset.item
        this.setData({
            showAlert: true,
            currentItemIndex: this.getIndexById(item.id, this.data.allMessages)
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({ lang: app.globalData.lang, navTitle: "sendAll", navigation: true })
        this.getPost()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})